//
//  MeatShopViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MyMeatShopViewController.h"
#import "MeatSaleListViewController.h"
#import "MeatSaveViewController.h"
#import "MeatShopGivingListViewController.h"
#import "MeatOrderListViewController.h"
#import "TransferManagerViewController.h"
#import "ShopBooksViewController.h"
#import "SaleDetailManagerViewController.h"
#import "TransferDetailManagerViewController.h"


#import "MyMeatShopHeader.h"
#import "MyMeatShopCell.h"

#import "MeatShopApi.h"
#import "MeatMarketApi.h"

@interface MyMeatShopViewController ()

@property (nonatomic, strong) MyMeatShopHeader *header;

@property (nonatomic, strong) UIButton *detailTypeBtn;

@property (nonatomic, strong) NSMutableArray *transicationTypeArr;

@property (nonatomic, assign) TransicationType transicationType;

@property (nonatomic, strong) NSArray *icons;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *details;


@end

@implementation MyMeatShopViewController

- (void)setupUI{
    
    [self.view addSubview:self.header];
    {
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        self.detailTypeBtn.mj_y = self.baseNavView.navTitleLabel.mj_y;
        self.detailTypeBtn.mj_x = (KScreenWidth - self.detailTypeBtn.width)/2.0;
        self.detailTypeBtn.height = self.baseNavView.navTitleLabel.height;
        [self.baseNavView addSubview:self.detailTypeBtn];
        self.baseNavView.backgroundColor = KClearColor;
        self.baseNavView.line.hidden = YES;
        [self.header addSubview:self.baseNavView];
    }
}


- (void)setupLayout{
    
    self.tableView.mj_y = _header.bottom;
    self.tableView.height = KScreenHeight - _header.bottom;
}

- (void)initData {
    
    NSArray *titleArr = @[@"全部",@"预售款",@"充值",@"养猪",@"买肉",@"提现",@"奖金"];
    
    for (int i = 0; i < 7; i ++) {
        PullDownMenuModel *model = [[PullDownMenuModel alloc] init];
        model.title = titleArr[i];
        model.isSelect = NO;
        [self.transicationTypeArr addObject:model];
    };
    
    //创建店铺
    [self requestMyShop];
}

//创建店铺 NO
- (void)requestCreateShopData{
    
    ShopCreateParam *param = [[ShopCreateParam alloc] init];
    param.shopName = @"一帆风顺";
    param.memberId = kMemberId;
    param.tribeId = @2;
    param.msg_content = @"不好意思，今天打烊了";
    param.imgName = @"xxxx";
    param.autograph = @"众生平等";
    [[[MeatShopApi sharedInstance] getShopCreateByParams:param] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//分割方案列表 Yes
- (void)requestSeparatePlan{
    
    [[[MeatShopApi sharedInstance] getShopSeparatePlan] subscribeNext:^(ShopSeparatePlanModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//分割方案详情 Yes
- (void)requestSeparatePlanDetail{
    
    [[[MeatShopApi sharedInstance] getShopSeparatePlanDetailByDivisionPlanId:@1] subscribeNext:^(ShopSeparateDetailModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//确认分割方案 NO 500
- (void)requestSeparatePlanAddDivisionPlan{
    
    [[[MeatShopApi sharedInstance] getShopSeparatePlanOkByShopId:@61 divisionPlanId:@5] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//分配方案（肉块分配 Yes
- (void)requestSeparatePlanList{

    ShopSeparateParam *param = [ShopSeparateParam new];
    param.shopId = @0;
    param.pigId = @0;
    param.pigPositionId = @0;
    param.page = @1;
    param.limit = @10;
    
    [[[MeatShopApi sharedInstance] getShopSparateByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//预留或销售 Yes
- (void)requestShopSaleOrSave{
    
    NSArray *advanceSaleIds = @[@0,@1];
    [[[MeatShopApi sharedInstance] getShopSaveOrSaleByAdvanceSaleType:@0 advanceSaleId:advanceSaleIds] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}

//我得肉铺 NO 500
- (void)requestMyShop{
    
    [[[MeatShopApi sharedInstance] getMyShopByTribeId:@2] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    }error:^(NSError * _Nullable error) {
        
    }];
}


//我得店铺销售 Yes
- (void)requestMyShopSale{
    
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = @1;
    param.limit = @10;
    
    [[[MeatShopApi sharedInstance] getMyShopSaleByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable x) {
    
    } error:^(NSError * _Nullable error) {
        
    }];
}

//我得店铺预留 Yes
- (void)requestMyShopSave{
    
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = @1;
    param.limit = @10;
    
    [[[MeatShopApi sharedInstance] getMyShopSaveByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
    
}

//上下架与促销 Yes
- (void)requestMyShopUpperShelfLowerShelf{
    
    ShopSalePromotionParam *param = [ShopSalePromotionParam new];
    param.advancesaleId = @[@0, @1];
    param.advanceSaleType = @0;
    param.promotionIf = @0;
    
    [[[MeatShopApi sharedInstance] getMyShopSalePromotionByParams:param] subscribeNext:^(NSNull * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//商品改价 NO 500
- (void)requestMyShopChagePrice{
    
    ShopChagePriceParam *param = [ShopChagePriceParam new];
    param.advancesaleId = @[@0, @1];
    param.pigPositionId = @[@0, @1];
    param.meatChunksId = @[@0, @1];
    param.advanceSaleType = @0;
    param.promotionIf = @0;
    
    [[[MeatShopApi sharedInstance] getMyShopChagePriceByParams:param] subscribeNext:^(NSNull * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//店铺账本 Yes
- (void)requestMyShopBook{
    
    [[[MeatShopApi sharedInstance] getMyShopBookByShopId:@1] subscribeNext:^(ShopBookModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//销售明细 Yes
- (void)requestMySaleDetail{
    
    [[[MeatShopApi sharedInstance] getMyShopSaleDetailByShopId:@1 pigId:@0 sellStatus:@0] subscribeNext:^(ShopSaleDetailModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//转让明细_未完成明细 NO
- (void)requestMyTranferDetail{
    
    [[[MeatShopApi sharedInstance] getMyTranferDetailNOByShopId:@1 pigId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//出售明细 No 600
- (void)requestMyShopSaleDetail{
    
    [[[MeatShopApi sharedInstance] getMySaleDetailByShopId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//订单详情 NO
- (void)requestMyOrderDetail{
    
    [[[MeatShopApi sharedInstance] getMyOrderDetailByOrderId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//转让明细_已完成明细 No
- (void)requestMySaleFinish{
    
    [[[MeatShopApi sharedInstance] getMyTranferDetailFinishByShopId:@1 pigId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//转让管理 NO
- (void)requestTranferManage{
    
    [[[MeatShopApi sharedInstance] getMyTranferManageByShopId:@1 pigId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//转让管理_改价 Yes
- (void)requestTranferManageChagePrice{
    
    [[[MeatShopApi sharedInstance] getMyTranferManageChagePriceByAssigShopId:@1 money:@100] subscribeNext:^(NSNull * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//转让管理_转让明细 NO
- (void)requestTranferDetail{
    
    [[[MeatShopApi sharedInstance] getMyTranferManageDetailByAssigShopId:@1 pigId:@1] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//留_一键赠送 NO
- (void)requestShopSave{
    
    [[[MeatShopApi sharedInstance] getMyTranferSaveByAdvanceSaleType:@1 advanceSaleId:@[@0,@1]] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//确认赠送 NO
- (void)requestSendComfirm{
    
    ShopSendComfirmParam *param = [ShopSendComfirmParam new];
    param.memberId = kMemberId;
    param.shopId = @1;
    param.advancesaleId = @[@1, @0];
    param.receiveAddress = @"xxxxx";
    
    [[[MeatShopApi sharedInstance] getMyShopSendComfirmByParams:param] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//赠送列表 NO
- (void)requestSendList{
    
    ShopSendListParam *param = [ShopSendListParam new];
    param.memberId = [NSString stringWithFormat:@"%@", kMemberId];
    param.shopId = @"1";
    param.page = @1;
    param.limit = @10;
    
    [[[MeatShopApi sharedInstance] getMyShopSendListByParams:param] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//查看赠送 NO
- (void)requestSendListCheck{
    
    [[[MeatShopApi sharedInstance] getSendCheckByShopId:@1 goodsOrderId:@[@0, @1]] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//撤回赠送 Yes
- (void)requestSendCallBack{
    
    [[[MeatShopApi sharedInstance] getSendCallBackByGoodsOrderId:@1] subscribeNext:^(NSNull * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//一键发单
- (void)requestSendOneKey{
    
    ShopOrderSendParam *param = [ShopOrderSendParam new];
    param.shopId = @1;
    param.pigId = @1;
    ShopOrderSendItem *item = [ShopOrderSendItem new];
    item.pigPositionId = @1;
    item.price = @100;
    item.oldPrice = @10;
    item.meatChunksId = @[@1];
    param.list = @[item];
    
    [[[MeatShopApi sharedInstance] getMyOrderSendByParams:param] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//全部订单 NO
- (void)requestOrderWhole{
    
    ShopOrderWholeParam *param = [ShopOrderWholeParam new];
    param.shopId = @1;
    param.pigId = @1;
    param.dropdownOptions = @0;
    param.page = @1;
    param.limit = @10;
    
    [[[MeatShopApi sharedInstance] getMyOrderWholeByParams:param] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//全部订单_设置购买人观看权限 Yes
- (void)requestOrderResetWatch{
    
    [[[MeatShopApi sharedInstance] getShopResetByPigId:@1] subscribeNext:^(ShopResetCameraModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//预定订单复制 Yes
- (void)requestOrderCopy{
    
    [[[MeatShopApi sharedInstance] getOrderCopyByPigId:@1] subscribeNext:^(ShopOrderCopyModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//买家申请更换收货地址. No
- (void)requestOrderLocationChage{
    
    [[[MeatShopApi sharedInstance] getShopChangeLocationByGoodsOrderId:@1 receiveAddress:@"xxxxxx"] subscribeNext:^(PigTypeModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

//冷链物流. NO
- (void)requestOrderColdLogistics{
    
    [[[MeatShopApi sharedInstance] getColdLogisticsByGoodsOrderId:@1] subscribeNext:^(NSNull * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}


//好友发单 NO
- (void)requestOrderFriend{
    
    [[[MeatShopApi sharedInstance] getOrderByShopId:@1] subscribeNext:^(ShopOrderFriendModel * _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - target action
- (void)detailTypeClick:(UIButton *)button {
    button.selected = !button.selected;
    
    if (button.selected) {
        @weakify(self);
        [[PullDownMenu sharedInstance] showWithFrame:CGRectMake(0, TopBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT) type:PullDownTypeTransicationTypes dataArr:self.transicationTypeArr completionBlock:^(NSString *text, NSInteger idx ,PullDownType pullWownType) {
            @strongify(self);
            if ([text isEqualToString:@"全部"]) {
                self.transicationType = TransicationTypeAll;
            } else if ([text isEqualToString:@"充值"]){
                self.transicationType = TransicationTypeTopUp;
            } else if ([text isEqualToString:@"养猪"]){
                self.transicationType = TransicationTypeAdoptPig;
            } else if ([text isEqualToString:@"预售款"]){
                self.transicationType = TransicationTypePresell;
            } else if ([text isEqualToString:@"奖金"]){
                self.transicationType = TransicationTypeReward;
            } else if ([text isEqualToString:@"提现"]){
                self.transicationType = TransicationTypeWithdraw;
            } else if ([text isEqualToString:@"买肉"]) {
                self.transicationType = TransicationTypeBugMeat;
            }
            //修改title和img的edge
            //            [self.detailTypeBtn setTitle:text forState:UIControlStateNormal];
            //            [self changeBtnEdge];
            //            self.detailTypeBtn.selected = !self.detailTypeBtn.selected;
            //            [self.dataArr removeAllObjects];
            //            self.currentPage = 1;
            //[self dataRequest];
        }];
    } else {
        [[PullDownMenu sharedInstance] hide];
    }
}



#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0: //销售
            {
                MeatSaleListViewController *vc = [[MeatSaleListViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
            
        case 1:
            {
                MeatSaveViewController *vc = [[MeatSaveViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
            
        default:
            {
                ShopBooksViewController *vc = [[ShopBooksViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
    }
}


#pragma mark - tableview.datascource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.icons.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyMeatShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyMeatShopCell"];
    cell.shopIcon.image = [UIImage imageNamed:self.icons[indexPath.row]];
    cell.titleLab.text = self.titles[indexPath.row];
    cell.detailLab.text = self.details[indexPath.row];
    
    return cell;
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.rowHeight = 90;
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_header.hidden = YES;
    [self.tableView registerClass:[MyMeatShopCell class] forCellReuseIdentifier:@"MyMeatShopCell"];
}


- (MyMeatShopHeader *)header
{
    if (!_header) {
        _header = [[MyMeatShopHeader alloc] init];
    }
    return _header;
}

- (NSMutableArray *)transicationTypeArr{
    
    if (!_transicationTypeArr) {
        _transicationTypeArr = [[NSMutableArray alloc] init];
    }
    return _transicationTypeArr;
}


- (UIButton *)detailTypeBtn {
    
    if (!_detailTypeBtn) {
        _detailTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailTypeBtn.frame = CGRectMake(0, 0, 300, 0);
        [_detailTypeBtn setTitle:@"我的肉铺（武清）" forState:UIControlStateNormal];
        [_detailTypeBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        _detailTypeBtn.titleLabel.font = mMediumFont(18);
        [_detailTypeBtn setImage:[UIImage imageNamed:@"meat_separate_up"] forState:UIControlStateNormal];
        [_detailTypeBtn setImage:[UIImage imageNamed:@"meat_separate_down"] forState:UIControlStateSelected];
        [_detailTypeBtn addTarget:self action:@selector(detailTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_detailTypeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, - _detailTypeBtn.imageView.image.size.width, 0, _detailTypeBtn.imageView.image.size.width)];
        [_detailTypeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _detailTypeBtn.titleLabel.bounds.size.width, 0, -_detailTypeBtn.titleLabel.bounds.size.width)];
    }
    return _detailTypeBtn;
}


- (NSArray *)icons{
    
    if (!_icons) {
        _icons = @[@"meat_separate_salemanager", @"meat_separate_state", @"meat_separate_order"];
    }
    return _icons;
}


- (NSArray *)titles{
    
    if (!_titles) {
        _titles = @[@"销售", @"预留", @"订单"];
    }
    return _titles;
}


- (NSArray *)details{
    
    if (!_details) {
        _details = @[@"销售副标题内容销售副标题内容销售", @"预留副标题内容预留副标题内容预留", @"订单副标题内容订单副标题内容订单"];
    }
    return _details;
}


@end
